<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\ArticleCategory;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Show;
use Dcat\Admin\Tree;

class ArticleCategoryController extends AdminController
{
    public function index(Content $content)
    {
        return $content->header('文章分类')
            ->body(function (Row $row) {
                $tree = new Tree(new ArticleCategory);
                $tree->branch(function ($branch) {
                    return "{$branch['id']} - {$branch['title']}";
                });
                $row->column(5, $tree);
            });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ArticleCategory(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('parent_id');
            $grid->column('title');
            $grid->column('order');
            $grid->column('seo_title');
            $grid->column('seo_keywords');
            $grid->column('seo_description');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter$filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ArticleCategory(), function (Show $show) {
            $show->field('id');
            $show->field('parent_id');
            $show->field('title');
            $show->field('order');
            $show->field('image');
            $show->field('seo_title');
            $show->field('seo_keywords');
            $show->field('seo_description');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ArticleCategory(), function (Form $form) {
            $articleCategoryModel = new \App\Models\ArticleCategory();
            $form->display('id');
            $form->select('parent_id', '上级')
                ->options($articleCategoryModel->selectOptions(null, '顶级'))
                ->saving(function ($v) {
                    return (int) $v;
                })
                ->default(0)
                ->required();
            $form->text('title', '名称')->required();
            $form->text('order', '排序')->required();
            $form->text('seo_title', 'SEO标题')
                ->saving(function ($v) {
                    return (string) $v;
                });
            $form->text('seo_keywords', 'SEO关键词')
                ->saving(function ($v) {
                    return (string) $v;
                });
            $form->textarea('seo_description', 'SEO描述')
                ->saving(function ($v) {
                    return (string) $v;
                });
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
